'\" et
.TH RM "1P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
rm
\(em remove directory entries
.SH SYNOPSIS
.LP
.nf
rm \fB[\fR-iRr\fB]\fI file\fR...
.P
rm -f \fB[\fR-iRr\fB]\fI \fB[\fIfile\fR...\fB]\fR
.fi
.SH DESCRIPTION
The
.IR rm
utility shall remove the directory entry specified by each
.IR file
argument.
.P
If either of the files dot or dot-dot are specified as the basename
portion of an operand (that is, the final pathname component) or if an
operand resolves to the root directory,
.IR rm
shall write a diagnostic message to standard error and do nothing more
with such operands.
.P
For each
.IR file
the following steps shall be taken:
.IP " 1." 4
If the
.IR file
does not exist:
.RS 4 
.IP " a." 4
If the
.BR \-f
option is not specified,
.IR rm
shall write a diagnostic message to standard error.
.IP " b." 4
Go on to any remaining
.IR files .
.RE
.IP " 2." 4
If
.IR file
is of type directory, the following steps shall be taken:
.RS 4 
.IP " a." 4
If neither the
.BR \-R
option nor the
.BR \-r
option is specified,
.IR rm
shall write a diagnostic message to standard error, do nothing more
with
.IR file ,
and go on to any remaining files.
.IP " b." 4
If
.IR file
is an empty directory,
.IR rm
may skip to step 2d.
If the
.BR \-f
option is not specified, and either the permissions of
.IR file
do not permit writing and the standard input is a terminal or the
.BR \-i
option is specified,
.IR rm
shall write a prompt to standard error and read a line from the
standard input. If the response is not affirmative,
.IR rm
shall do nothing more with the current file and go on to any remaining
files.
.IP " c." 4
For each entry contained in
.IR file ,
other than dot or dot-dot, the four steps listed here (1 to 4) shall be
taken with the entry as if it were a
.IR file
operand. The
.IR rm
utility shall not traverse directories by following symbolic links into
other parts of the hierarchy, but shall remove the links themselves.
.IP " d." 4
If the
.BR \-i
option is specified,
.IR rm
shall write a prompt to standard error and read a line from the
standard input. If the response is not affirmative,
.IR rm
shall do nothing more with the current file, and go on to any remaining
files.
.RE
.IP " 3." 4
If
.IR file
is not of type directory, the
.BR \-f
option is not specified, and either the permissions of
.IR file
do not permit writing and the standard input is a terminal or the
.BR \-i
option is specified,
.IR rm
shall write a prompt to the standard error and read a line from the
standard input. If the response is not affirmative,
.IR rm
shall do nothing more with the current file and go on to any remaining
files.
.IP " 4." 4
If the current file is a directory,
.IR rm
shall perform actions equivalent to the
\fIrmdir\fR()
function defined in the System Interfaces volume of POSIX.1\(hy2017 called with a pathname of the current
file used as the
.IR path
argument. If the current file is not a directory,
.IR rm
shall perform actions equivalent to the
\fIunlink\fR()
function defined in the System Interfaces volume of POSIX.1\(hy2017 called with a pathname of the current
file used as the
.IR path
argument.
.RS 4 
.P
If this fails for any reason,
.IR rm
shall write a diagnostic message to standard error, do nothing more
with the current file, and go on to any remaining files.
.RE
.P
The
.IR rm
utility shall be able to descend to arbitrary depths in a file
hierarchy, and shall not fail due to path length limitations (unless an
operand specified by the user exceeds system limitations).
.SH OPTIONS
The
.IR rm
utility shall conform to the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 12.2" ", " "Utility Syntax Guidelines".
.P
The following options shall be supported:
.IP "\fB\-f\fP" 10
Do not prompt for confirmation. Do not write diagnostic messages or
modify the exit status in the case of no file operands, or in the case
of operands that do not exist. Any previous occurrences of the
.BR \-i
option shall be ignored.
.IP "\fB\-i\fP" 10
Prompt for confirmation as described previously. Any previous
occurrences of the
.BR \-f
option shall be ignored.
.IP "\fB\-R\fP" 10
Remove file hierarchies. See the DESCRIPTION.
.IP "\fB\-r\fP" 10
Equivalent to
.BR \-R .
.SH OPERANDS
The following operand shall be supported:
.IP "\fIfile\fR" 10
A pathname of a directory entry to be removed.
.SH STDIN
The standard input shall be used to read an input line in response to
each prompt specified in the STDOUT section. Otherwise, the standard
input shall not be used.
.SH "INPUT FILES"
None.
.SH "ENVIRONMENT VARIABLES"
The following environment variables shall affect the execution of
.IR rm :
.IP "\fILANG\fP" 10
Provide a default value for the internationalization variables that are
unset or null. (See the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 8.2" ", " "Internationalization Variables"
for the precedence of internationalization variables used to determine
the values of locale categories.)
.IP "\fILC_ALL\fP" 10
If set to a non-empty string value, override the values of all the
other internationalization variables.
.IP "\fILC_COLLATE\fP" 10
.br
Determine the locale for the behavior of ranges, equivalence classes,
and multi-character collating elements used in the extended regular
expression defined for the
.BR yesexpr
locale keyword in the
.IR LC_MESSAGES
category.
.IP "\fILC_CTYPE\fP" 10
Determine the locale for the interpretation of sequences of bytes of
text data as characters (for example, single-byte as opposed to
multi-byte characters in arguments) and the behavior of character
classes within regular expressions used in the extended regular
expression defined for the
.BR yesexpr
locale keyword in the
.IR LC_MESSAGES
category.
.IP "\fILC_MESSAGES\fP" 10
.br
Determine the locale used to process affirmative responses, and the
locale used to affect the format and contents of diagnostic messages
and prompts written to standard error.
.IP "\fINLSPATH\fP" 10
Determine the location of message catalogs for the processing of
.IR LC_MESSAGES .
.SH "ASYNCHRONOUS EVENTS"
Default.
.SH STDOUT
Not used.
.SH STDERR
Prompts shall be written to standard error under the conditions
specified in the DESCRIPTION and OPTIONS sections. The prompts shall
contain the
.IR file
pathname, but their format is otherwise unspecified. The standard
error also shall be used for diagnostic messages.
.SH "OUTPUT FILES"
None.
.SH "EXTENDED DESCRIPTION"
None.
.SH "EXIT STATUS"
The following exit values shall be returned:
.IP "\00" 6
Each directory entry was successfully removed, unless its removal was
canceled by a non-affirmative response to a prompt for confirmation.
.IP >0 6
An error occurred.
.SH "CONSEQUENCES OF ERRORS"
Default.
.LP
.IR "The following sections are informative."
.SH "APPLICATION USAGE"
The
.IR rm
utility is forbidden to remove the names dot and dot-dot in order to
avoid the consequences of inadvertently doing something like:
.sp
.RS 4
.nf

rm -r .*
.fi
.P
.RE
.P
Some implementations do not permit the removal of the last link to an
executable binary file that is being executed; see the
.BR [EBUSY] 
error in the
\fIunlink\fR()
function defined in the System Interfaces volume of POSIX.1\(hy2017. Thus, the
.IR rm
utility can fail to remove such files.
.P
The
.BR \-i
option causes
.IR rm
to prompt and read the standard input even if the standard input is not
a terminal, but in the absence of
.BR \-i
the mode prompting is not done when the standard input is not a
terminal.
.SH EXAMPLES
.IP " 1." 4
The following command:
.RS 4 
.sp
.RS 4
.nf

rm a.out core
.fi
.P
.RE
.P
removes the directory entries:
.BR a.out
and
.BR core .
.RE
.IP " 2." 4
The following command:
.RS 4 
.sp
.RS 4
.nf

rm -Rf junk
.fi
.P
.RE
.P
removes the directory
.BR junk
and all its contents, without prompting.
.RE
.SH RATIONALE
For absolute clarity, paragraphs (2b) and (3) in the DESCRIPTION of
.IR rm
describing the behavior when prompting for confirmation, should be
interpreted in the following manner:
.sp
.RS 4
.nf

if ((NOT f_option) AND
    ((not_writable AND input_is_terminal) OR i_option))
.fi
.P
.RE
.P
The exact format of the interactive prompts is unspecified. Only the
general nature of the contents of prompts are specified because
implementations may desire more descriptive prompts than those used on
historical implementations. Therefore, an application not using the
.BR \-f
option, or using the
.BR \-i
option, relies on the system to provide the most suitable dialog
directly with the user, based on the behavior specified.
.P
The
.BR \-r
option is historical practice on all known systems. The synonym
.BR \-R
option is provided for consistency with the other utilities in this volume of POSIX.1\(hy2017
that provide options requesting recursive descent through the file
hierarchy.
.P
The behavior of the
.BR \-f
option in historical versions of
.IR rm
is inconsistent. In general, along with ``forcing'' the unlink without
prompting for permission, it always causes diagnostic messages to be
suppressed and the exit status to be unmodified for nonexistent
operands and files that cannot be unlinked. In some versions, however,
the
.BR \-f
option suppresses usage messages and system errors as well.
Suppressing such messages is not a service to either shell scripts or
users.
.P
It is less clear that error messages regarding files that cannot be
unlinked (removed) should be suppressed. Although this is historical
practice, this volume of POSIX.1\(hy2017 does not permit the
.BR \-f
option to suppress such messages.
.P
When given the
.BR \-r
and
.BR \-i
options, historical versions of
.IR rm
prompt the user twice for each directory, once before removing its
contents and once before actually attempting to delete the directory
entry that names it. This allows the user to ``prune'' the file
hierarchy walk. Historical versions of
.IR rm
were inconsistent in that some did not do the former prompt for
directories named on the command line and others had obscure prompting
behavior when the
.BR \-i
option was specified and the permissions of the file did not permit
writing. The POSIX Shell and Utilities
.IR rm
differs little from historic practice, but does require that prompts be
consistent. Historical versions of
.IR rm
were also inconsistent in that prompts were done to both standard
output and standard error. This volume of POSIX.1\(hy2017 requires that prompts be done to
standard error, for consistency with
.IR cp
and
.IR mv ,
and to allow historical extensions to
.IR rm
that provide an option to list deleted files on standard output.
.P
The
.IR rm
utility is required to descend to arbitrary depths so that any file
hierarchy may be deleted. This means, for example, that the
.IR rm
utility cannot run out of file descriptors during its descent (that is,
if the number of file descriptors is limited,
.IR rm
cannot be implemented in the historical fashion where one file
descriptor is used per directory level). Also,
.IR rm
is not permitted to fail because of path length restrictions, unless an
operand specified by the user is longer than
{PATH_MAX}.
.P
The
.IR rm
utility removes symbolic links themselves, not the files they refer to,
as a consequence of the dependence on the
\fIunlink\fR()
functionality, per the DESCRIPTION. When removing hierarchies with
.BR \-r
or
.BR \-R ,
the prohibition on following symbolic links has to be made explicit.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIrmdir\fR\^"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "Chapter 8" ", " "Environment Variables",
.IR "Section 12.2" ", " "Utility Syntax Guidelines"
.P
The System Interfaces volume of POSIX.1\(hy2017,
.IR "\fIremove\fR\^(\|)",
.IR "\fIrmdir\fR\^(\|)",
.IR "\fIunlink\fR\^(\|)"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
